<?php
namespace Home\Controller;
use Home\Common\Common;

class AttendanceinfoController extends Common {
    public function index(){
        $staff= M('attendanceinfo');
        if (empty($_GET['search']) || empty($_GET['value'])){
            $where='';
        }else{
            $where =array('`stf_name` like "%'.$_GET['value'].'%" or `ad_data` like "%'.$_GET['value'].'%"')  ;
            $this->assign('value',$_GET['value']);
        }

        $count = $staff->where($where)->join('staffinfo on attendanceinfo.stf_id=staffinfo.stf_id')->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $staff->where($where)->join('staffinfo on attendanceinfo.stf_id=staffinfo.stf_id')->limit($page->firstRow.','.$page->listRows)->order('attendanceinfo.ad_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();
    }

    public function mypunch(){
        $staff= M('attendanceinfo');
        if (empty($_GET['search']) || empty($_GET['value'])){
            $where=array('attendanceinfo.stf_id'=>$_SESSION['aid']);
        }else{
            $where =array(' `ad_data` like "%'.$_GET['value'].'%"')  ;
            $this->assign('value',$_GET['value']);
        }

        $count = $staff->where($where)->join('staffinfo on attendanceinfo.stf_id=staffinfo.stf_id')->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $staff->where($where)->join('staffinfo on attendanceinfo.stf_id=staffinfo.stf_id')->limit($page->firstRow.','.$page->listRows)->order('attendanceinfo.ad_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();
    }
    public function punch(){
        $staff= D('attendanceinfo');
        if(empty($_POST['submit'])){
            $this->display();
        }else{
            $attendance=M('attendanceinfo');
            $time1=M('time');
            $where1=array('id'=>1);
            $where=array('attendanceinfo.stf_id'=>$_SESSION['aid'],'ad_data'=>date('Y-m-d',time()));
            $attendance=$attendance->where($where)->select();
            $time1=$time1->where($where1)->select();

            if(empty($attendance)){
                //第一次打卡
                $_POST['stf_id']=$_SESSION['aid'];
                $_POST['state']=5;
                $_POST['person']='暂无';
                $staff->add($_POST);
                $this->success('打卡成功!',U('mypunch'));
            }else if ($attendance[0]['ad_endword']==''||$attendance[0]['ad_endword']!=''){
                //第二次或更多打卡,
                //判断考勤状态 1全勤，2迟到，3早退，4迟到+早退，5旷工
                //如下为上班、下班打卡
                $attendance[0]['ad_endword']=date('H:i:s',time());
                $time= $attendance[0]['ad_endword'];
                $sss = explode(":", $attendance[0]['ad_startword']);
                $arr=$sss[0].$sss[1].$sss[2];
                $ssr = explode(":", $attendance[0]['ad_endword']);
                $srr=$ssr[0].$ssr[1].$ssr[2];
                //如下为规定迟到、早退时间。默认时间：
                $kk1=explode(":",$time1[0]['starttime']);//8:30
                $kk2=explode(":",$time1[0]['endtime']);//18:30
                $dd1=$kk1[0].$kk1[1].$kk1[2];
                $dd2=$kk2[0].$kk2[1].$kk2[2];
                //根据两次打卡时间进行判断考勤状态
                if(intval($arr)<=intval($dd1) && intval($srr)>=intval($dd2)){
                    $_POST['state']=1;
                }
                if(intval($arr)>=intval($dd1) && intval($srr)>=intval($dd2)){
                    $_POST['state']=2;
                }
                if(intval($arr)<=intval($dd1) && intval($srr)<=intval($dd2)){
                    $_POST['state']=3;
                }
                if(intval($arr)>=intval($dd1) && intval($srr)<=intval($dd2)){
                    $_POST['state']=4;
                }
                if(intval($arr)>=intval($dd2)){
                    $_POST['state']=5;
                }

                $attendance[0]['ad_person']='admin';
                $staff->where($where)->save(array('ad_endword'=>$time,'state'=>$_POST['state']));
                $this->success('打卡成功!',U('mypunch'));
            }else{
                $this->error('参数错误！');
            }
        }
    }

    public function change(){
        if(empty($_POST['submit'])){
            $time=M('time');
            $info = $time->where(array('id'=>1))->find();
            $this->assign('info',$info);
            $this->display();
        }else{
            $time=M('time');
            $time->where(array('id'=>1))->save($_POST);
            $this->success('修改成功!',U('change'));
        }

    }

    public function edit(){
        if (empty($_POST['submit'])){
            if (empty($_GET['ad_id'])) {
                $this->error('参数错误!', U('index'));
            }

            $admin = M('attendanceinfo');
            $info = $admin->where('`ad_id`='.$_GET['ad_id'])->find();
            $this->assign('info',$info);
            $this->display();
        }else{

            $staff = D('attendanceinfo');
            if($_POST['state']=='全勤'){
                $_POST['state']=1;
            }
            if($_POST['state']=='迟到'){
                $_POST['state']=2;
            }
            if($_POST['state']=='早退'){
                $_POST['state']=3;
            }
            if($_POST['state']=='早退+迟到'){
                $_POST['state']=4;
            }
            if($_POST['state']=='旷工'){
                $_POST['state']=5;
            }
            if($_POST['state']=='事假'){
                $_POST['state']=6;
            }
            $staffinfo=M('staffinfo');
            $staffinfo=$staffinfo->where('`stf_id`='.$_SESSION['aid'])->select();
            $_POST['person']=$staffinfo[0]['stf_name'];
            $staff->where('`ad_id`='.$_POST['ad_id'])->save($_POST);
            $this->success('修改成功!',U('index'));

        }

    }


}